<?php
class Mdlt extends CI_Model
{
	function __construct()
	{
		parent::__construct();
		$this->tablename = 'dlt14128';
	}
	
	function get_one($id) {
		$item = new stdClass();
		$query = $this->db->query('select * from '.$this->tablename.' where id = '.$id);
		if ($query->num_rows() > 0) {
			$item = current($query->result());
		}
		$query->free_result();
		return $item;
	}

	function get_all($order='') {
		$item = new stdClass();
		if($order)
			$query = $this->db->query('select * from '.$this->tablename.' order by '.$order.' desc');
		else
			$query = $this->db->query('select * from '.$this->tablename);
		if ($query->num_rows() > 0) {
			$item = $query->result();
		}
		$query->free_result();
		return $item;
	}

	function get_qishu() {
		$item = new stdClass();
		$query = $this->db->query('select qishu from '.$this->tablename.' group by qishu ');
		if ($query->num_rows() > 0) {
			$item = $query->result();
		}
		$query->free_result();
		return $item;
	}

	function pagination($uid = 0, $pindex = 0, &$totalcount = 0, $perpage = 10) {
		$item = new stdClass();
		$startnumber = $pindex * $perpage;
		$this->db->limit($perpage, $startnumber);
		$query = $this->db->query('select * from `yun_records` where uk = '.$uid.' order by created desc limit '.$startnumber.','.$perpage);
		if ($query->num_rows() > 0) {
			$item = $query->result();
		}

		$query1 = $this->db->query('select rid from `yun_records` where uk = '.$uid);
		$totalcount = $query1->num_rows() ;
		$query->free_result();
		$query1->free_result();
		return $item;
	}

    function exist_id($uk){
		$item = 0 ;
		$query = $this->db->query('select id from '.$this->tablename.' where id = '.$uk);
		if ($query->num_rows() > 0) {
			$item = 1;
		}
		return $item;
	}

    function get_ids($qs,$id,$qiu='red'){
		$item = new stdClass();
		$id = (string)$id ;
		$ids = array() ;
		$query = $this->db->query('select id,red,blue  from '.$this->tablename.' where qishu='.$qs);
		$item = $query->result() ;
		if($qiu == 'red'){
			foreach($item as $v){
				if(strpos($v->red,$id) !== false){
					$iid = $v->id ;
					$ids[$iid] = $iid;
				}
			}
		}else{
			foreach($item as $v){
				if(strpos($v->blue,$id) !== false){
					$iid = $v->id ;
					$ids[$iid] = $iid;
				}
			}
		}

		return $ids;
	}

    function get_num($qs,$id,$qiu='red'){
		$item = new stdClass();
		$num = 0 ;
		$id = (string)$id ;
		$query = $this->db->query('select id,red,blue from '.$this->tablename.' where `qishu`= '.$qs);
		$item = $query->result() ;
		if($qiu == 'red'){
			foreach($item as $v){
				if(strpos($v->red,$id) !== false){
					$num += 1;
					//$num +=$this->get_nums($id,$v->red,$v->blue) ;
				}
				//$query1 = $this->db->query('select id from '.$this->tablename.' where '.$qiu.' like "%'.$id.'%" and id ='.$v->id);
				/*
				$query1 = $this->db->query('select id from '.$this->tablename.' where instr('.$qiu.','.$id.')>0 and id ='.$v->id);
				if ($query1->num_rows() > 0) {
					$num += 1;
				}
				*/
			}
		}else{
			foreach($item as $v){
				if(strpos($v->blue,$id) !== false){
					$num += 1;
					//$num += $this->get_blue_num($id,$v->blue);
				}
			}
		}

		return $num;
	}

	// ---------获得彩票注数--------------------
	public function get_nums($ns,$red,$blue){
		//  ---------篮球注数-----------------------------
		if(strpos($blue,')')!==false){
			$blue_arr = explode(' ',$blue) ;
			$blue_count = count($blue_arr) ;
			$blue_scale = $blue_count - 1 ;
		}else{
			$blue_arr = explode(' ',$blue) ;
			$blue_count = count($blue_arr) ;
			$blue_scale = ($blue_count)*($blue_count-1)/2 ;
		}

		//  ---------红球注数-----------------------------
		if(strpos($red,')')=== false){
			$red_arr = explode(' ',$red) ;
			$red_count = count($red_arr) ;
			$red_scale = $red_count*($red_count-1)*($red_count-2)*($red_count-3)*($red_count-4)/120 ;
		}else{
			$dan = $ns_key = 0 ;
			$red_arr = explode(' ',$red) ;
			foreach($red_arr as $ks=>$vs){
				if(strpos($vs,')')!==false){
					$dan = $ks+1;
				}
				if(strpos($vs,$ns)!==false){
					$ns_key = $ks+1 ;
				}
			}

			$yuma = count($red_arr) - $dan ;       //  剩余球，余码
			if($ns_key <= $dan){    // 是胆码
				$tuo = 5 - $dan ;
				$bottom = $top = 1 ;
				for($i=0;$i<$tuo;$i++){
					$bottom = $bottom*($yuma-$i) ;
					$top = $top*($tuo-$i) ;
				}
				$red_scale = $bottom / $top ;
			}else{
				$tuo = 5 - $dan - 1 ;
				$bottom = $top = 1 ;
				for($i=0;$i<$tuo;$i++){
					$bottom = $bottom*(($yuma-1)-$i) ;
					$top = $top*($tuo-$i) ;
				}
				$red_scale = $bottom / $top ;
			}
		}
		$num = $red_scale*$blue_scale ;
		return $num ;
	}

	// ---------获得彩票注数--------------------
	public function get_blue_num($ns,$blue){
		//  ---------篮球注数-----------------------------
		if(strpos($blue,')')!==false){
			$dan = $ns_key = 0 ;
			$blue_arr = explode(' ',$blue) ;
			foreach($blue_arr as $ks=>$vs){
				if(strpos($vs,')')!==false){
					$dan = $ks+1;
				}
				if(strpos($vs,$ns)!==false){
					$ns_key = $ks+1 ;
				}
			}

			if($ns_key <= $dan){    // 是胆码
				$blue_count = count($blue_arr) ;
				$blue_scale = $blue_count - 1 -1 ;
			}else{
				$blue_scale = 1 ;
			}
		}else{
			$blue_arr = explode(' ',$blue) ;
			$blue_count = count($blue_arr) ;
			$blue_scale = ($blue_count)*($blue_count-1)/2 ;
		}
		return $blue_scale ;
	}

    function insert($arr){
		if(!$this->exist_id($arr['id'])){                                    // 防止重复记录
			$this->db->insert($this->tablename,$arr);
			return  $this->db->insert_id(); 
		}else{
			log_message('error', '-----------------用户--'.$arr['username'].'['.$arr['uid'].']'.' 已存在库里-------------------------------');
			return false;
		}
    }

    function update($id,$arr)
    {
        $this->db->where('id',$id);
        $this->db->update($this->tablename,$arr);
    }

    function delete($id){
        $this->db->where('id',$id);
        $this->db->delete($this->tablename);
    }

}